package org.byteDance.l103;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @Author yu
 * @Date 2022/3/5
 */
public class Solution {

    public static void main(String[] args) {

        TreeNode treeNode = new TreeNode(15);
        TreeNode treeNode1 = new TreeNode(7);
        TreeNode treeNode2 = new TreeNode(20, treeNode, treeNode1);
        TreeNode treeNode3 = new TreeNode(9);
        TreeNode treeNode4 = new TreeNode(3, treeNode3, treeNode2);

        Solution solution = new Solution();
        solution.zigzagLevelOrder3(treeNode4);

    }

    /**
     * DFS
     *
     * @param root
     * @return java.util.List<java.util.List<java.lang.Integer>>
     */
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();
        traversal(root, res, 0);
        return res;
    }

    private void traversal(TreeNode node, List<List<Integer>> res, int level) {
        if (node == null) {
            return;
        }

        if (res.size() == level) {
            res.add(new LinkedList<>());
        }

        // 判断层数是奇数还是偶数
        // 1、奇数进入第一个条件，add(0, node.val) 可以实现层反转，当链表索引 0 的位置有值的时候，再次向 0 的位置添加数据，索引 0 位置原有的数据会自动向后移动
        // 2、偶数进入第二个条件，正常添加近链表
        if ((level & 1) == 1) {
            res.get(level).add(0, node.val);
        } else {
            res.get(level).add(node.val);
        }

        traversal(node.left, res, level + 1);
        traversal(node.right, res, level + 1);
    }

    /**
     * BFS
     *
     * @param root
     */
    private List<List<Integer>> zigzagLevelOrder2(TreeNode root) {
        List<List<Integer>> res = new LinkedList<>();

        if (root == null) {
            return res;
        }
        int level = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {

            List<Integer> list = new LinkedList<>();
            int size = queue.size();

            // 遍历层节点，利用队列先进先出原则
            while (size > 0) {
                TreeNode node = queue.poll();
                if (level % 2 == 0) {
                    list.add(node.val);
                } else {
                    list.add(0, node.val);
                }
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                size--;
            }

            res.add(list);
            level++;
        }

        return res;
    }

    private List<List<Integer>> zigzagLevelOrder3(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        int m = 0;
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new LinkedList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();

                if (m % 2 == 0) {
                    list.add(0, node.val);
                } else {
                    list.add(node.val);
                }
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            System.out.println(list);
            lists.add(list);
            m++;
        }
        return lists;
    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}